package edu.pku.sei.modeler.uml.commands;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicActivities.ActivityEdge;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Message;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Transition;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.Dependencies.Realization;
import edu.pku.sei.modeler.model.uml.Interfaces.InterfaceRealization;
import edu.pku.sei.modeler.model.uml.Kernel.Association;
import edu.pku.sei.modeler.model.uml.Kernel.Generalization;
import edu.pku.sei.modeler.model.uml.Kernel.PackageImport;
import edu.pku.sei.modeler.model.uml.Kernel.PackageMerge;
import edu.pku.sei.modeler.model.uml.UseCases.Extend;
import edu.pku.sei.modeler.model.uml.UseCases.Include;
import edu.pku.sei.modeler.uml.commands.activity.DeleteActivityEdgeFlowCommand;
import edu.pku.sei.modeler.uml.commands.interactiveoverview.InteractionOverviewControlFlowDeleteCommand;
import edu.pku.sei.modeler.uml.commands.sequence.UMLMessageDeleteCommand;
import edu.pku.sei.modeler.uml.commands.statemachine.DeleteTransitionCommand;
import edu.pku.sei.modeler.uml.commands.umlclass.UMLDependencyDeleteCommand;
import edu.pku.sei.modeler.uml.commands.umlclass.UMLInterfaceRealizationDeleteCommand;
import edu.pku.sei.modeler.uml.commands.umlclass.UMLPackageImportDeleteCommand;
import edu.pku.sei.modeler.uml.commands.umlclass.UMLPackageMergeDeleteCommand;
import edu.pku.sei.modeler.uml.commands.umlclass.UMLRealizationDeleteCommand;
import edu.pku.sei.modeler.uml.commands.usecase.UMLExtendDeleteCommand;
import edu.pku.sei.modeler.uml.commands.usecase.UMLIncludeDeleteCommand;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

public class UMLConnectionDeleteCommandFactory {

	public static final UMLConnectionDeleteCommandFactory eINSTANCE = new UMLConnectionDeleteCommandFactory();

	private UMLConnectionDeleteCommandFactory() {

	}

	public Command createCommand(MetaLink element, boolean isDeleteFromDiagram) {
		// CompoundCommand command = new CompoundCommand();
		//		
		// if (element.getModelElement() instanceof Association) {
		// command.add(new UMLAssociationDeleteCommand((MetaLink)
		// (element),isDeleteFromDiagram));
		// }
		//
		//		
		// if (element.getModelElement() instanceof Extend) {
		// command.add(new
		// UMLExtendDeleteCommand((MetaLink)element,isDeleteFromDiagram));
		// }
		//		
		// if (element.getModelElement() instanceof Generalization) {
		// command.add(new
		// UMLGeneralizaitonDeleteCommand((MetaLink)element,isDeleteFromDiagram
		// ));
		// }
		//		
		// if (element.getModelElement() instanceof Include) {
		// command.add(new
		// UMLIncludeDeleteCommand((MetaLink)element,isDeleteFromDiagram));
		// }
		//		
		// if (element.getModelElement() instanceof Message) {
		// command.add(new
		// UMLMessageDeleteCommand((MetaLink)element,isDeleteFromDiagram));
		// }
		//		
		// if (element.getModelElement() instanceof InterfaceRealization) {
		// command.add(new
		// UMLInterfaceRealizationDeleteCommand((MetaLink)element
		// ,isDeleteFromDiagram));
		// }
		//		
		// if (element.getModelElement() instanceof Realization) {
		// command.add(new
		// UMLRealizationDeleteCommand((MetaLink)element,isDeleteFromDiagram));
		// }
		//		
		// if (element.getModelElement() instanceof PackageImport) {
		// command.add(new
		// UMLPackageImportDeleteCommand((MetaLink)element,isDeleteFromDiagram
		// ));
		// }
		//		
		// if (element.getModelElement() instanceof PackageMerge) {
		// command.add(new
		// UMLPackageMergeDeleteCommand((MetaLink)element,isDeleteFromDiagram));
		// }
		//		
		// // if (element.getModelElement() instanceof Usage) {
		// // command.add(new UMLUsageDeleteCommand((MetaLink)element));
		// // }
		// //
		//		
		// if (element.getModelElement() instanceof Dependency) {
		// MetaNode node = ((MetaLink)element).getSource();
		// String diagramType;
		//			
		// if (node != null) {
		// diagramType = node.getDiagram().getType();
		// }else {
		// diagramType = ((MetaLink)element).getTarget().getDiagram().getType();
		// }
		//			
		//			
		// if (diagramType.equals("UML-usecase")) {
		// command.add(new UMLCommentLinkDeleteCommand((MetaLink)element,
		// isDeleteFromDiagram));
		// }
		// else {
		// command.add(new
		// UMLDependencyDeleteCommand((MetaLink)element,isDeleteFromDiagram));
		// }
		//			
		// }
		UMLConnectionDeleteCommand cmd = new UMLConnectionDeleteCommand(
				element, isDeleteFromDiagram);
		MetaModelElement connectionModel = element.getModelElement();
		MetaModelElement sourceModel = element.getSource().getModelElement();
		MetaModelElement targetModel = element.getTarget().getModelElement();
		if (element.getModelElement() instanceof Association) {
			cmd.setModelCmd(new UMLAssociationDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof Extend) {
			cmd.setModelCmd(new UMLExtendDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof Generalization) {
			cmd.setModelCmd(new UMLGeneralizaitonDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof Include) {
			cmd.setModelCmd(new UMLIncludeDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof Message) {
			cmd.setModelCmd(new UMLMessageDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof InterfaceRealization) {
			cmd.setModelCmd(new UMLInterfaceRealizationDeleteCommand(
					connectionModel, sourceModel, targetModel));
		} else if (element.getModelElement() instanceof Realization) {
			cmd.setModelCmd(new UMLRealizationDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof PackageImport) {
			cmd.setModelCmd(new UMLPackageImportDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof PackageMerge) {
			cmd.setModelCmd(new UMLPackageMergeDeleteCommand(connectionModel,
					sourceModel, targetModel));
		} else if (element.getModelElement() instanceof ActivityEdge) {
			if (ModelPackage.DIAGRAM_UML_ACTIVITY.equals(element.getSource().getDiagram().getType())) {
				cmd.setModelCmd(new DeleteActivityEdgeFlowCommand(connectionModel, sourceModel, targetModel));
			} else if (ModelPackage.DIAGRAM_UML_INTERACTIONOVERVIEW.equals(element.getSource().getDiagram().getType())) {
				cmd.setModelCmd(new InteractionOverviewControlFlowDeleteCommand(connectionModel, sourceModel, targetModel));
			}
		} else if (connectionModel instanceof Transition) {
			cmd.setModelCmd(new DeleteTransitionCommand(connectionModel, sourceModel, targetModel));
		}

		// if (element.getModelElement() instanceof Usage) {
		// command.add(new UMLUsageDeleteCommand((MetaLink)element));
		// }
		//		

		else if (element.getModelElement() instanceof Dependency) {
			MetaNode node = ((MetaLink) element).getSource();
			String diagramType;

			if (node != null) {
				diagramType = node.getDiagram().getType();
			} else {
				diagramType = ((MetaLink) element).getTarget().getDiagram()
						.getType();
			}

			if (diagramType.equals("UML-usecase")) {
				cmd.setModelCmd(new UMLCommentLinkDeleteCommand(
						connectionModel, sourceModel, targetModel));
			} else {
				cmd.setModelCmd(new UMLDependencyDeleteCommand(connectionModel,
						sourceModel, targetModel));
			}
		}
		return cmd;
	}

}
